package utils

import (
	"os"
	"path"
	"runtime"
	"strings"
)

func GetServerName() (hostname string) {
	hostname, _ = os.Hostname()
	if hostname == "" {
		hostname = "unknown"
	}
	return
}

func GetFilePath() (filePath string) {
	_, filename, _, _ := runtime.Caller(1)
	filePath = path.Join(path.Dir(filename))
	return
}

func substr(s string, pos, length int) string {
	runes := []rune(s)	
	l := pos + length
	if l > len(runes) {	
		l = len(runes)	
	}
	return string(runes[pos:l])
}

func GetParentDirectory(dirctory string) string {
	return substr(dirctory, 0, strings.LastIndex(dirctory, "/"))
}

// 判断所给路径文件/文件夹是否存在
func Exists(path string) bool {
	_, err := os.Stat(path) //os.Stat获取文件信息
	if err != nil {
		if os.IsExist(err) {
			return true
		}
		return false
	}
	return true
}
